package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.*;
import cn.wolfcode.crm.mapper.AccidentDetailMapper;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.mapper.InsuranceMapper;
import cn.wolfcode.crm.mapper.NewCaseMapper;
import cn.wolfcode.crm.query.impl.NewCaseQuery;
import cn.wolfcode.crm.service.INewCaseService;
import cn.wolfcode.crm.util.LogicException;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class NewCaseServiceImpl implements INewCaseService {
    @Autowired
    NewCaseMapper newCaseMapper;
    @Autowired
    AccidentDetailMapper accidentDetailMapper;
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    InsuranceMapper insuranceMapper;


    public void saveOrUpdate(NewCase record) {

        if (record.getId() != null) {
            newCaseMapper.updateByPrimaryKey(record);
            //检查当前状态,如果是已报案就设置事故明细表
            if (record.getState() == 3) {
                AccidentDetail detail = new AccidentDetail();
                detail.setId(record.getId());
                detail.setCheck(false);
                detail.setState(false);
                detail.setPlace(record.getPlace());
                detail.setTel(record.getTel());
                detail.setCarnumber(record.getCarnumber());
                detail.setEmployee(record.getEmpname());
                detail.setNumbering(record.getNumbering());
                System.out.println(detail);
                accidentDetailMapper.updateByPrimaryKey(detail);
            }
        } else {
            newCaseMapper.insert(record);
            //检查当前状态,如果是已报案就设置事故明细表
            if (record.getState() == 3) {
                AccidentDetail detail = new AccidentDetail();
                detail.setCheck(false);
                detail.setState(false);
                detail.setPlace(record.getPlace());
                detail.setTel(record.getTel());
                detail.setCarnumber(record.getCarnumber());
                detail.setEmployee(record.getEmpname());
                detail.setNumbering(record.getNumbering());
                System.out.println(detail);
                accidentDetailMapper.insert(detail);
            }
        }

    }

    public NewCase get(Long id) {
        return newCaseMapper.selectByPrimaryKey(id);
    }

    public List<NewCase> getAll() {
        return newCaseMapper.selectAll();
    }


    public PageResult query(NewCaseQuery qo) {
        int rows = newCaseMapper.queryForCount(qo);
        if (rows == 0) {
            new PageResult(rows, Collections.EMPTY_LIST);
        }
        List<NewCase> result = newCaseMapper.queryForList(qo);
        return new PageResult(rows, result);
    }

    /**
     * 检查报案表是否合格
     * 如果合格就把状态改为受理成功
     *
     * @param id
     */
    public void changeState(Long id) {
        //----------------------内容完整性审核----------------------------
        NewCase newCase = newCaseMapper.selectByPrimaryKey(id);
        if (newCase.getNumbering() == null || newCase.getName() == null || newCase.getGender() == null || newCase.getCarnumber() == null
                || newCase.getPlace() == null || newCase.getTel() == null || newCase.getTime() == null || newCase.getEmpname() == null) {
            //将状态改成拒绝受理并抛出异常
            throw new LogicException("表单不完整,清完善表单");
        }
        //------------------------内容审核------------------------------
        //1.拿到报案表中的报案人名称
        String name = newCase.getName();
        //2.用报案人名称查询出有没有这个用户
        Customer customer = customerMapper.selectByName(name);
        if (customer == null) {
            /*3.如果查出来的customer为null则抛出异常:没有该用户,请检查报案人名称(并且把状态改成没有该保单)(状态改成拒绝)
            把状态改成0(没有该保单)然后return终止方法*/
            throw new LogicException("没有该用户,请检查保单信息是否与用户信息匹配");
        }
        //4.如果有这个用户就根据这个用户的名字查询和车牌查询保单号,如果保单号为空就将该用户移交至用户资源池
        //获取当前保单号
        String numbering = newCase.getNumbering();
        //当别的对象关联了多个表的时候不可以实用*来查询ヽ(･ω･´ﾒ)ヽ(･ω･´ﾒ)
        Insurance insurance = insuranceMapper.selectBySn(numbering);
        if (insurance == null) {
            //把状态改成0(没有该保单)
            throw new LogicException("没有该保单,请检查保单号");
        }
        //4.1如果有保单那么就差车牌是否对应
        //4.2获取保单的车牌
        CarInformation carInformation = insurance.getCar();
        String carnumber = carInformation.getCarnumber();
        //4.3判断车牌是否相等
        if (!carnumber.equals(newCase.getCarnumber())) {//字符串比较实用equalsヽ(●-`Д´-)ノ
            throw new LogicException("车牌不匹配");
        }
        /*5.如果有该保单就创建一个new date 和保单创建时间加年限对比大小
        创建当前时间*/
        Date now = new Date();
        //获取该保单的截止日期,并对比
        Date endDate = insurance.getEndDate();
        if (endDate.getTime() < now.getTime()) {
            //6.如果过期就把状态改成保单失效并且将用户移至资源池(用户状态为false)
            throw new LogicException("保单失效");
        }
        //7.以上都没问题就将状态改成受理成功
        newCaseMapper.changeState(id, 3);
        //创建事故明细表对象
        newCase.setState(3);
        saveOrUpdate(newCase);
    }
}
